#!/bin/bash
# 获取当期git分支的tag，如果有v-前缀，则取v-后面的数字作为版本号，没有符合的tag，结束脚本
tag=$(git describe --tags --abbrev=0)
if [[ $tag == v* ]]; then
  ver=${tag:1}
else
  echo "no v- prefix tag found"
  exit 1
fi

yarn run build
# exit if yarn build failed
if [ $? -ne 0 ]; then
  echo "yarn build failed"
  exit 1
fi

# 获取当前脚本所在的绝对路径
current_dir=$(pwd)

# 源文件夹路径，这里假设dist就在当前目录下
source_dir="../dist"

# 目标文件夹路径
target_dir="$current_dir/html"

mkdir -p "$target_dir"

# 使用cp命令结合-r（递归）和-f（强制覆盖）选项来进行复制操作
cp -rf "$source_dir"/* "$target_dir"

echo "复制操作已完成。"

image=registry.cn-hangzhou.aliyuncs.com/bookshare-ruiya/oj-frontend

tag=v-$ver

# build and push
docker build -t $image:latest -t $image:$tag .
docker push $image:latest
docker push $image:$tag

rm -rf $target_dir

echo 'pushed image to '$image':v-'$ver
